//
//  SearchViewController.m
//  GitBucket
//
//  Created by 3024 on 15/11/2.
//  Copyright © 2015年 ios25. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchResultTableViewController.h"

@interface SearchViewController ()
{
    
    //搜索控制器
    UISearchController *_searchController;
    
    //创建历史记录数组
    NSMutableArray *_recentArr;
}
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //设置视图边缘
    self.edgesForExtendedLayout = UIRectEdgeNone;

    //https://api.github.com/search/repositories?q=louisgdm
    if ([[NSData alloc]initWithContentsOfFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"recent.text"]] == nil) {
        _recentArr = [NSMutableArray new];
    }else{
        _recentArr = [NSMutableArray arrayWithContentsOfFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"recent.text"]];

    }
    
    //创建搜索结果表视图控制器
    SearchResultTableViewController *srtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchResultTableViewController"];
    
    
    srtvc.transferBlock = ^(SearchResultTableViewController* svc){
      //历史数组加入字符串对象
        [_recentArr addObject:svc.searchStr];
        //保存路径
        NSString *FilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"recent.text"];
        //保存
        if ([_recentArr writeToFile:FilePath atomically:YES]) {
            NSLog(@"保存记录成功");
        }
        [self.tableView reloadData];
    };
    
    
    //搜索控制器的使用
    _searchController = [[UISearchController alloc]initWithSearchResultsController:srtvc];
    
    _searchController.searchResultsUpdater = srtvc;
    _searchController.delegate =srtvc;
    _searchController.searchBar.delegate = srtvc;
    
    //自动调整搜索框大小
    [_searchController.searchBar sizeToFit];
    
    //搜索框显示在导航栏
    self.navigationItem.titleView = _searchController.searchBar;
    
    _searchController.hidesNavigationBarDuringPresentation = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _recentArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"recent" forIndexPath:indexPath];
    
    cell.textLabel.text = _recentArr[indexPath.row];
    
    return cell;
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    SearchResultTableViewController *srtvc = segue.destinationViewController;
    UITableViewCell *cell = sender;
   
    srtvc.searchStr = cell.textLabel.text;
    
}


@end
